package edu.uni.inictel.systems.siobd.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class ServletTest
 */
@WebServlet("/test")
public class ServletTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletTest() {
        super();
        
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method =	request.getParameter("method");
		
		System.out.println("----Llego a Metodo doGet. Method: "+method);
		
		if(method!=null) {
			switch (method) {
			case "prueba":
				doAccion(request,response);
				break;
			case "redirect":
				doRedirect(request,response);
				break;
			case "redirect2":
				doRedirect(request,response);
				break;
			case "empleados":
				doEmpleados(request,response);
				break;
			default:
				doDefault(request, response);
				break;
			}
		}else {
			doDefault(request, response);
		}		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method =	request.getParameter("method");
		
		System.out.println("----Llego a Metodo doPost. Method: "+method);
		
		if(method!=null) {
			switch (method) {
			case "prueba":
				doAccion(request,response);
				break;
			case "redirect":
				doRedirect(request,response);
				break;
			case "redirect2":
				doRedirect(request,response);
				break;
			case "empleados":
				doEmpleados(request,response);
				break;
			default:
				doDefault(request, response);
				break;
			}
		}else {
			doDefault(request, response);
		}				
	}
	protected void doAccion(HttpServletRequest request, HttpServletResponse response)
			throws ServletException,IOException	{
		response.setContentType("text/html;charset=UTF-8");
		
		String msj="Accion: RESPUESTA RECIBIDA EN TESTSERVLET";
		System.out.println(msj);
		sendHTML(response, msj);
	}
	
	protected void doDefaultTest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException,IOException	{
		response.setContentType("text/html;charset=UTF-8");
		String msj="Default: RESPUESTA RECIBIDA EN TESTSERVLET";
		System.out.println(msj);
		sendHTML(response, msj);
	}
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException,IOException	{
		
		request.getRequestDispatcher("E404.jsp").forward(request, response);
	
	}
	protected void doRedirect(HttpServletRequest request, HttpServletResponse response)
			throws ServletException,IOException	{
//		response.setContentType("text/html;charset=UTF-8");
		String msj="Redirect: RESPUESTA RECIBIDA EN TESTSERVLET ,metodos DoRedirect";
		request.setAttribute("respuesta", "Se ha cargado la paginad Desde el servlet,doRedirect");
		System.out.println(msj);
//		sendHTML(response, msj);
		request.getRequestDispatcher("test.jsp").forward(request, response);
	}
	
	protected void doRedirect2(HttpServletRequest request, HttpServletResponse response)
			throws ServletException,IOException	{

		String msj="Redirect2: RESPUESTA RECIBIDA EN TESTSERVLET";
		request.setAttribute("respuesta", "Se ha cargado la pagina Desde el servlet, en Redirect2");
		System.out.println(msj);
		
		request.getRequestDispatcher("test2.jsp").forward(request, response);
	}
	
	protected void doEmpleados(HttpServletRequest request, HttpServletResponse response)
			throws ServletException,IOException	{

		
//		request.setAttribute("respuesta", "Se ha cargado la paginad Desde el servlet,doRedirect");

//		sendHTML(response, msj);
		System.out.println("Se llegó al metodo doEmpleados");
		request.getRequestDispatcher("testEmpleados.jsp").forward(request, response);
	}
		
	private void sendHTML(HttpServletResponse response,String msj) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html");
			
			response.getWriter().write(msj);
			
		} catch (Exception e) {
			System.out.println("sendHTML: No se pudo enviar petición");
		}
	}
	
	@SuppressWarnings("unused")
	private void sendPlain(HttpServletResponse response,String msj) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/plain");
			
			response.getWriter().write(msj);
			
		} catch (Exception e) {
			System.out.println("sendHTML: No se pudo enviar petición");
		}
	}	 
}
